package com.carkeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carkeeper.dto.CUserDTO;
import com.carkeeper.dto.CarDTO;
import com.carkeeper.dto.StoreDTO;
import com.carkeeper.entity.*;
import com.carkeeper.mapper.OrderMapper;
import com.carkeeper.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrderLogService orderLogService;
    @Autowired
    private CUserFeign cUserFeign;
    @Autowired
    private StoreFeign storeFeign;
    @Autowired
    private CarFeign carFeign;

    /*
     * 创建订单
     * */
    @Override
    public Boolean createOrder(OrderPO order) {
        return orderMapper.insert(order) > 0;
    }

    /*
     * 删除订单
     * */
    @Override
    public Boolean delById(Long id) {
        return orderMapper.deleteById(id) > 0;
    }
    /*
     * 获取订单信息
     * */

    @Override
    public OrderPO getOrder(Long id) {
        OrderPO order = orderMapper.selectById(id);

        ArrayList<ServicePO> servicePOList = new ArrayList<>();
        List<OrderDetailPO> orderDetailPoByDao = orderDetailService.getOrderDetail(id);
        for (OrderDetailPO orderDetailPO : orderDetailPoByDao) {
            ServicePO servicePO = new ServicePO();
            servicePO.setName(orderDetailPO.getName());
            servicePO.setCode(orderDetailPO.getCode());
            servicePO.setNormalPrice(orderDetailPO.getPrice());
            servicePOList.add(servicePO);
        }
        order.setServicePOList(servicePOList);
        order.setOrderLogPO(orderLogService.getByOrderId(id));

        if (order.getUserId() != null) {
            CUserDTO userDTO = cUserFeign.findById(order.getUserId());
            order.setUser(userDTO);
        }
        if (order.getStoreId() != null) {
            StoreDTO storeDTO = storeFeign.queryStoreById(order.getStoreId());
            order.setStore(storeDTO);
        }
        if (StringUtils.isNotEmpty(order.getCarCode())) {
            CarDTO carDTO = new CarDTO();
            carDTO.setCode(order.getCarCode());
            CarDTO carDTOByDao = carFeign.findByCode(carDTO);
            order.setCar(carDTOByDao);
        }
        return order;
    }
    /*
     * 更新订单信息
     * */

    @Override
    public Boolean update(OrderPO order) {
        return orderMapper.updateById(order) > 0;
    }

    /*
     * 获取订单分页数据
     * */

    @Override
    public Page<OrderPO> getOrderPage(Integer page, Integer pagesize, Integer type,
                                      String contact, String orderStatus,
                                      String createTimeStart, String createTimeEnd, String storeName, String code, String payStatus) {
        Page<OrderPO> poPage = new Page<>(page, pagesize);
        LambdaQueryWrapper<OrderPO> wrapper = new LambdaQueryWrapper<>();
        if (type != null) {
            wrapper.eq(OrderPO::getType, type);
        }
        if (!StringUtils.isEmpty(contact)) {
            wrapper.like(OrderPO::getContact, contact);
        }
        if (!StringUtils.isEmpty(orderStatus)) {
            wrapper.eq(OrderPO::getPayStatus, orderStatus);
        }
        if (!StringUtils.isEmpty(createTimeStart)) {
            wrapper.ge(OrderPO::getCreateTime, createTimeStart);
        }
        if (!StringUtils.isEmpty(createTimeEnd)) {
            wrapper.le(OrderPO::getCreateTime, createTimeEnd);
        }
        if (!StringUtils.isEmpty(storeName)) {
            wrapper.like(OrderPO::getStoreName, storeName);
        }
        if (StringUtils.isNotEmpty(code)) {
            wrapper.like(OrderPO::getCode, code);
        }
        if (StringUtils.isNotEmpty(payStatus)) {
            wrapper.eq(OrderPO::getPayStatus, payStatus);
        }
        if (StringUtils.isNotEmpty(orderStatus)) {
            wrapper.eq(OrderPO::getOrderStatus, orderStatus);
        }
        Page<OrderPO> poPage1 = orderMapper.selectPage(poPage, wrapper);
        return poPage1;
    }

    @Override
    public List<OrderPO> findAll(Long storeId, String orderStatus, String payStatus) {
        LambdaQueryWrapper<OrderPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (storeId != null) {
            lambdaQueryWrapper.eq(OrderPO::getStoreId, storeId);
        }
        if (StringUtils.isNotEmpty(orderStatus)) {
            lambdaQueryWrapper.eq(OrderPO::getOrderStatus, orderStatus);
        }
        if (StringUtils.isNotEmpty(payStatus)) {
            lambdaQueryWrapper.eq(OrderPO::getPayStatus, payStatus);
        }
        return orderMapper.selectList(lambdaQueryWrapper);
    }
}
